import Cookies from 'js-cookie';
import {
  APP_TOGGLE_SIDEBAR,
  APP_CLOSE_SIDEBAR,
  APP_TOGGLE_DEVICE
} from '../../const/mutationTypes';
import { SIDEBAR_STATUS } from '../../const/cookieTypes';

const state = {
  sidebar: {
    opened: Cookies.get(SIDEBAR_STATUS) ? !!+Cookies.get(SIDEBAR_STATUS) : true,
    withoutAnimation: false
  },
  device: 'desktop'
};

const mutations = {
  [APP_TOGGLE_SIDEBAR]: (state) => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened) {
      Cookies.set(SIDEBAR_STATUS, 1);
    } else {
      Cookies.set(SIDEBAR_STATUS, 0);
    }
  },
  [APP_CLOSE_SIDEBAR]: (state, withoutAnimation) => {
    Cookies.set(SIDEBAR_STATUS, 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
  [APP_TOGGLE_DEVICE]: (state, device) => {
    state.device = device;
  }
};

const actions = {
  toggleSideBar({ commit }) {
    commit(APP_TOGGLE_SIDEBAR);
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit(APP_CLOSE_SIDEBAR, withoutAnimation);
  },
  toggleDevice({ commit }, device) {
    commit(APP_TOGGLE_DEVICE, device);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
